#pragma once

#include <iostream>
#include <string>
#include <ctemplate/template.h>

// #include "oj_model.hpp"
#include "oj_model2.hpp"


namespace NS_view
{
    using namespace NS_model;

    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View()
        {}
        ~View()
        {}
        void AllExpandHtml(std::vector<Question>& questions, std::string *html)
        {
            //表格显示
            std::string src_html = template_path + "all_questions.html";
            //形成字典
            ctemplate::TemplateDictionary root("all_questions");
            for(auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }

            //获取渲染网页
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //开始完成渲染功能
            tpl->Expand(html,&root);
        }
        void OneExpandHtml(const Question& q, std::string *html)
        {
            //1.路径
            std::string src_html = template_path + "one_question.html";
            ctemplate::TemplateDictionary root("one_question");
            //2.形成数据字典
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);
            //渲染网页
            ctemplate::Template *tp1 = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            tp1->Expand(html,&root);
        }
    };
}